package com.hzvdvmc.autosale.socket.manager;

import com.hzvdvmc.autosale.constant.CmdEnum;
import com.hzvdvmc.autosale.socket.SocketClientManager;
import com.hzvdvmc.autosale.util.StringUtil;
import com.vilyever.socketclient.SocketClient;

/**
 * Created by william on 2017/12/13.
 * 指令集
 */

public class CommandByteManager {
    /*
    包活
     */
    public static byte KEEP_LIVE = 0x39;
    /*
    心跳包
     */
    public static byte HEARTBEAT = 0x40;
    /*
    签到
     */
    public static byte SIGN = 0x41;
    /*
    支付成功
     */
    public static byte PAY_SUCCESS = 0x42;
    /*
    上报出货结果
     */
    public static byte REPORT_SALE_RESULT = 0x43;
    /*
    请求支付
     */
    public static byte PAY = 0x44;
    /*
    远程重启
     */
    public static byte CONTROL_RESTART = 0x50;
    /*
    远程升级
     */
    public static byte CONTROL_UPDATE = 0x51;
    /*
    远程修改现金价格
     */
    public static byte CONTROL_MODIFY_PRICE = 0x52;
    /*
    远程补货
     */
    public static byte CONTROL_SUPPLEMENT = 0x53;
    /*
    远程一键补货
     */
    public static byte CONTROL_SUPPLEMENT_BY_ONE = 0x54;
    /*
    商品上架表更新
     */
    public static byte CONTROL_SHOPLIST_REFRESH = 0x55;

    /*
    货道设置
     */
    public static byte CONTROL_SHOPWAY_SETTING = 0x56;
    /*
    发布广告方案
     */
    public static byte CONTROL_ADVERTISEMENT = 0x57;
    /*
    系统参数更改
     */
    public static byte CONTROL_SYSTEM_PARAM = 0x58;
    /*
    人工清除故障
     */
    public static byte REPORT_CLEAR_ERROR = 0x60;
    /*
    机器通电
     */
    public static byte REPORT_POWER_ON = 0x61;
    /*
      机器断电
     */
    public static byte REPORT_POWER_OFF = 0x62;
    /*
    开门
     */
    public static byte REPORT_OPEN_DOOR = 0x63;
    /*
    关门
     */
    public static byte REPORT_CLOSE_DOOR = 0x64;
    /*
    一键补货
     */
    public static byte REPORT_SUPPLEMENT_BY_ONE = 0x65;
    /*
    详细补货
     */
    public static byte REPORT_SUPPLEMENT_BY_DETAIL = 0x66;
    /*
    货道最大容量
     */
    public static byte REPORT_SHOPWAY_COMPACITY = 0x67;
    /*
    货道商品表
     */
    public static byte REPORT_SHOPWAY_LIST = 0x68;
    /*
    更新广告方案
     */
    public static byte REPORT_UPDATE_ADVERTISEMENT = 0x69;
    /*
    参数需修改
     */
    public static byte REQUEST_MODIFY_PARAMS = 0x70;
    /*
    有出货
     */
    public static byte REQUEST_HAVE_SALE = 0x71;
    /*
    有价格更改
     */
    public static byte REQUEST_MODIFY_PRICE = 0x72;
    /*
    有库存更改
     */
    public static byte REQUEST_MODIFY_INVENTORY = 0x73;
    /*
    广告方案更新
     */
    public static byte REQUEST_UPDATE_ADVERTISEMENT = 0x74;
    /*
    有商品上架更新
     */
    public static byte REQUEST_MODIFY_SHOPSTATE = 0x75;
    /*
    上报故障
     */
    public static byte REPORT_ERROR = 0x7E;

    private static SocketClient socketClient;
    private static CommandByteManager manager;

    public static CommandByteManager getInstance() {
        if (manager == null) {
            manager = new CommandByteManager();
            socketClient = SocketClientManager.getInstance().getSocketClient();
        }
        return manager;
    }

    public CommandByteManager() {
    }

    private SocketClient getSocketClient() {
        return socketClient;
    }

    private byte[] getFullData(byte[] data) {
        return data;
    }

    /**
     * 完整上传数据
     * @param command 指令
     * @param data 中间的有效数据
     * @return
     */
    private String getFullCommand(CmdEnum command, String data) {
        StringBuilder sb = new StringBuilder();
        sb.append(CmdEnum.START.getValue());
        sb.append(command.getValue());
        sb.append(StringUtil.convertStringToHex(data));
        return sb.toString();
    }

    /*
    保活
     */
    public void keepLiveRequest() {
        byte[] data = new byte[]{KEEP_LIVE};
        getSocketClient().sendData(getFullData(data));
    }



}
